﻿

namespace QFlow.Windows.Forms
{
    using System;
    using System.Linq;
    using System.Windows.Forms;
    using QFlow.Data;

    public partial class NewCustomer : QFlow.Utils.BaseForm
    {
        bool _New;
        int _CustomerId;
        public NewCustomer(bool dialog, QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
            _New = true;
            if (dialog)
                btnCancel.Visible = true;
        }

        public NewCustomer(int customerId, QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
            _CustomerId = customerId;
            Customer customer = this.Context.Customers.SingleOrDefault(c => c.CustomerID == _CustomerId);
            txtName.Text = customer.Name;
            txtAddress.Text = customer.Address;
            txtPhone.Text = customer.Phone;
            txtMobile.Text = customer.Mobile;
            _New = false;
            btnCancel.Visible = true;
            this.Text = QFlow.Windows.Properties.Resources.FRMEditCustomer;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (superValidator1.Validate())
            {
                try
                {
                    if (_New)
                    {
                        Customer customer = new Customer();
                        customer.Name = txtName.Text;

                        if (!string.IsNullOrEmpty(txtAddress.Text))
                            customer.Address = txtAddress.Text;
                        if (!string.IsNullOrEmpty(txtPhone.Text))
                            customer.Phone = txtPhone.Text;
                        if (!string.IsNullOrEmpty(txtMobile.Text))
                            customer.Mobile = txtMobile.Text;

                        customer.Accounts.Add(new Account());

                        this.Context.Customers.InsertOnSubmit(customer);
                        this.Context.SubmitChanges();
                    }
                    else
                    {
                        Customer customer = this.Context.Customers.SingleOrDefault(c => c.CustomerID == _CustomerId);
                        customer.Name = txtName.Text;

                        if (!string.IsNullOrEmpty(txtAddress.Text))
                            customer.Address = txtAddress.Text;
                        if (!string.IsNullOrEmpty(txtPhone.Text))
                            customer.Phone = txtPhone.Text;
                        if (!string.IsNullOrEmpty(txtMobile.Text))
                            customer.Mobile = txtMobile.Text;

                        this.Context.SubmitChanges();
                    }
                    PostSave();
                }
                catch
                {
                    MessageBox.Show(QFlow.Windows.Properties.Resources.MBCantSaveChanges, QFlow.Windows.Properties.Resources.MBErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
        }

        private void PostSave()
        {
            this.Close();
        }

        private void NewCustomer_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (txtName.CausesValidation)
            {
                txtName.CausesValidation = false;
                this.Close();
            }
        }
    }
}
